using System;
using Microsoft.Xna.Framework;

namespace SpriteAnimations.Graphics.Animations
{
    class CompositeAnimation : SpriteAnimation
    {
        //
        // Fields
        //
        private SpriteAnimation[] animations;

        //
        // Constructors
        //
        public CompositeAnimation(params SpriteAnimation[] animations)
        {
            this.animations = animations;
        }

        //
        // Virtual Methods
        //
        public override void SetAnimatedSprite(Sprite sprite)
        {
            foreach (SpriteAnimation animation in animations)
            {
                animation.SetAnimatedSprite(sprite);
            }

            base.SetAnimatedSprite(sprite);
        }

        public override void Animate(GameTime gameTime, SpriteParameters animatedParameters)
        {
            foreach (SpriteAnimation animation in animations)
            {
                animation.Animate(gameTime, animatedParameters);
            }

            // Are we finished?
            bool allAnimationsFinished = true;
            foreach (SpriteAnimation animation in animations)
            {
                if (!animation.IsFinished)
                {
                    allAnimationsFinished = false;
                    break;
                }
            }

            if (allAnimationsFinished)
                base.IsFinished = true;
        }
    }
}
